﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JobsBoardDAL.DTOs;
using JobsBoardDAL;
using System.Windows;

namespace JobBoardAdminPanel.ViewModels
{
    public class CompanyViewModel : ViewModelBase
    {
        private CompanyDTO company;

        public CompanyViewModel(CompanyDTO company)
        {
            this.company = company;

            if (company == null)
            {
                this.company = new CompanyDTO();
            }
        }

        public int ID
        {
            get
            {
                return this.company.ID;
            }
        }
        public string Name
        {
            get
            {
                return this.company.CompanyName;
            }
            set
            {
                if (this.company.CompanyName != value)
                {
                    this.company.CompanyName = value;
                    OnPropertyChanged("CompanyName");
                }
            }
        }
        public byte[] Logo
        {
            get
            {
                return this.company.Logo;
            }
            set
            {
                if(this.company.Logo != value)
                {
                    this.company.Logo = value;
                    OnPropertyChanged("Logo");
                }
            }
        }
        public string Description
        {
            get
            {
                return this.company.Description;
            }
            set
            {
                if(this.company.Description != value)
                {
                    this.company.Description = value;
                    OnPropertyChanged("Description");
                }
            }
        }
        public string WebSite
        {
            get
            {
                return this.company.WebSite;
            }
            set
            {
                if(this.company.WebSite != value)
                {
                    this.company.WebSite = value;
                    OnPropertyChanged("WebSite");
                }
            }
        }
        public string Email
        {
            get
            {
                return this.company.Email;
            }
            set
            {
                if(this.company.Email != value)
                {
                    this.company.Email = value;
                    OnPropertyChanged("Email");
                }
            }
        }
        public string Phone
        {
            get
            {
                return this.company.Phone;
            }
            set
            {
                if(this.company.Phone != value)
                {
                    this.company.Phone = value;
                    OnPropertyChanged("Phone");
                }
            }
        }

        internal void Update()
        {
            if (!Validate())
            {
                MessageBox.Show("Wrong parameters(NameLength, phonelength <= 50, WebSite, email length <= 150)");
                return;
            }

            try
            {
                JobsBoardCRUD.EditCompany(this.company);
            }
            catch
            {
                MessageBox.Show("Update unseccesful");
                return;
            }
        }

        private bool Validate()
        {
            bool valid = this.Name.Length <= 50 && this.WebSite.Length <= 150 && this.Email.Length <= 150 && this.Phone.Length <= 50;

            return valid;
        }

        internal void Delete()
        {
            try
            {
                JobsBoardCRUD.DeleteCompany(this.company);
            }
            catch
            {
                MessageBox.Show("Update unseccesful. Company refered by other object.");
                return;
            }
        }

        internal void Add()
        {
            if (!Validate())
            {
                MessageBox.Show("Wrong parameters(NameLength, phonelength <= 50, WebSite, email length <= 150)");
                return;
            }

            try
            {
                JobsBoardCRUD.AddCompany(this.company);
            }
            catch(Exception e)
            {
                MessageBox.Show("Update unseccesful");
                throw e;
                return;
            }
        }
    }
}
